/**
 *
 */
angular.module('myApp')

    .controller('queryCtrl', function ($scope, $http, $httpParamSerializerJQLike, $log, OpenService, CheckboxService,
                                       CacheService, limitToFilter, filterFilter,myConstant) {
        //视图层变量viewModel
        var vm = $scope.vm = {};
        vm.constant = myConstant;
        //变量初始化
        //分页数据
        vm.pagination = {
            pageSize: 10,
            pageNum: 1,
            list:{}
        };
        //查询条件
        vm.findBean = {};

        //缓存数据初始化(需要缓存的key请自定义)
        /*数据格式{
         key1 :value1,
         key2:value2
         }*/
        vm.cached = {
            CUSTPROPERTY:{},//客户属性
            PERSONAL_RISK_KW: {},//个人关键字
            MERCHANT_RISK_KW: {},//商户关键字
            PERSONAL_RISK_TYPE:{},//个人风险类型
            MERCHANT_RISK_TYPE:{},//商户风险类型
            INFORMATION_LEVEL:{}//信息级别

        };
        //批量获取缓存
        var keys = [];
        for (var key in  vm.cached) {
            keys.push(key);
        }
        $log.debug('批量查询缓存');
        $http.post(config.ctx + '/specially/dataDic/cache/get', keys, config.jsonHeader)
            .then(function (response) {
                var tempMap = response.data.object;
                for (var key in vm.cached) {
                    if (!angular.isUndefined(tempMap[key])) {
                        vm.cached[key] = tempMap[key];
                    }
                }
            }, function (response) {
                $log.error('获取数据%s失败', keys);
            })
        //CacheService.initCache(vm.cached);

        /*将前面缓存的数据格式化
         [{key:value},{key:value}]*/
        vm.getCache = function (key) {
            return CacheService.getCache(key);
        };

        //获取对象数据[user,user,user]
        vm.getObj = function (key, input) {
            return $http.post(config.ctx + '/base/cache/getAll',
                $httpParamSerializerJQLike({cacheKey: key}))
                .then(function (response) {
                    return limitToFilter(filterFilter(response.data.object, input), 6);
                }, function (response) {
                    $log.error('获取数据%s失败', cacheKey);
                })
        };


        vm.resetForm = resetForm;

        /*------------------以上配置通用---------------------*/


        vm.queryDetail = queryDetail;
        vm.updateChecked = updateChecked;
        /*------------------以上方法名可选择性通用---------------------*/

        function resetForm() {
            vm.findBean = {};
        }

        function updateChecked($event, id) {
            CheckboxService.updateChecked($event, id);
            $log.debug(id);
            $log.debug(CheckboxService.getChecked());
        }

        //查询
        function queryDetail(){
            var queryForm = document.getElementById('queryForm');
            vm.url = angular.element(queryForm).prop('action');
            vm.pagination.list = null;
            if(vm.findBean.cusProperty && vm.findBean.keyWord && vm.findBean.info && vm.findBean.riskType){
                //判断结束时间不能早于开始时间
                if(vm.findBean.startTime!=null && vm.findBean.endTime!=null && new Date(vm.findBean.startTime)> new Date(vm.findBean.endTime)){
                    alert("开始时间不能晚于结束时间");
                    return;
                }
                //查询数据显示
                var  postData = {};
                postData.cusProperty = vm.findBean.cusProperty;
                postData.keyWord = vm.findBean.keyWord;
                postData.info = vm.findBean.info;
                postData.riskType = vm.findBean.riskType;
                postData.startTime = vm.findBean.startTime;
                postData.endTime = vm.findBean.endTime;
                postData.pageIndex = vm.pagination.pageNum;
                postData.pageSize = vm.pagination.pageSize;
                $http.post(vm.url,postData,config.jsonHeader).then(function(response){
                    var data = response.data;
                    if(data.success){
                        vm.pagination.list = data.sumObject
                    }else{
                        alert(data.msg);
                    }
                })
            }else{
                alert("请填写完整查询信息！");
            }
        }
        //选中项改变事件
        $("#q_type").change(function(){
            var v = $("#q_type").val();
            //string:1000001
            v = v.substr(v.indexOf(":")+1);
            if(v =="01"){//个人
                vm.cached.Q_TYPE_CODE = vm.cached.PERSONAL_RISK_KW;
                vm.cached.RISK_TYPE_CODE = vm.cached.PERSONAL_RISK_TYPE;
            }else{//02:商户
                vm.cached.Q_TYPE_CODE =  vm.cached.MERCHANT_RISK_KW;
                vm.cached.RISK_TYPE_CODE = vm.cached.MERCHANT_RISK_TYPE;
            }
        });

        vm.enumCode={
            mobileno:"手机号",
            mac:"MAC地址",
            imei:"Imei",
            ip:"ip",
            address:"收货地址",

            bank_no:"银行帐/卡号",
            open_bank:"开户行",
            cus_name:"客户姓名",
            doc_code:"身份证件号码",
            cus_name_en:"商户英文名称",

            telephone:"固定电话",
            rec_bank_no:"收款银行帐/卡号",
            rec_open_bank:"收款账/卡号开户行",

            email:"邮箱",
            validdate:"有效期",
            level:"风险信息等级",
            occurtimeb:"风险事件发生时间",

            occurtimee:"风险事件结束时间",
            occurchan:"风险事件发生渠道",
            valid_status:"有效性",

            regname:"商户名称",
            cusname:"商户简称",
            cus_code:"商户编码",
            doc_type:"法人证件类型",
            doc_code:"法人证件号码",

            leg_rep_name:"法定代表人（负责人）姓名",
            leg_doc_code:"法定代表人（负责人）证件号码",
            leg_doc_name:"法定代表人姓名",
            leg_doc_type:"法定代表人证件类型",

            cont_name:"商户联系人",
            cont_phone:"商户联系电话",
            share_holder:"股东信息",

            out_service_name:"外包服务机构名称",
            out_service_card_type:"外包服务机构法人证件类型",
            out_service_card_code:"外包服务机构法人证件号码",
            out_service_leg_card_type:"外包服务机构法定代表人证件类型",
            out_service_leg_card_code:"外包服务机构法定代表人证件号码",

            url:"网址",
            serverip:"服务器IP",
            icp:"ICP备案编号",

            org_id:"上报机构",
            card_code:"法人证件号码 ",
            card_type:"法人证件类型"
        };//card_code  card_type

    })
;



